package ar.edu.unq.wannaplay.panels;

import java.util.Locale;

import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.model.PropertyModel;

import ar.edu.unq.wannaplay.main.WPApplication;
import ar.edu.unq.wannaplay.pages.WPPage;

public class ChooseLanguagePanel extends NonServicedPanel {

    private static final long serialVersionUID = 1L;

    private Class<? extends WPPage> wPPage;

    public ChooseLanguagePanel(final String id, final Class<? extends WPPage> aWPPage) {
        super(id);
        this.initialize();
        this.setWPPage(aWPPage);

    }

    private void initialize() {
        this.add(new LocaleDropDownChoice("localeSelect"));
        // Label label = new Label("label",
        // "eliga una acción desde el menú de la izquierda");
        // this.add(label);
    }

    private final class LocaleDropDownChoice extends DropDownChoice {
        private static final long serialVersionUID = 1L;

        /**
         * Construct.
         * 
         * @param id
         *            component id
         */
        public LocaleDropDownChoice(final String id) {
            super(id, WPApplication.LOCALES, new LocaleChoiceRenderer());

            // set the model that gets the current locale, and that is used for
            // updating the current locale to property 'locale' of FormInput
            this.setModel(new PropertyModel(ChooseLanguagePanel.this, "locale"));
        }

        /**
         * @see org.apache.wicket.markup.html.form.DropDownChoice#onSelectionChanged(java.lang.Object)
         */
        @Override
        public void onSelectionChanged(final Object newSelection) {
            // note that we don't have to do anything here, as our property
            // model allready calls FormInput.setLocale when the model is
            // updated

            // force re-render by setting the page to render to the bookmarkable
            // instance, so that the page will be rendered from scratch,
            // re-evaluating the input patterns etc
            this.setResponsePage(ChooseLanguagePanel.this.getWPPage());
        }

        /**
         * @see org.apache.wicket.markup.html.form.DropDownChoice#wantOnSelectionChangedNotifications()
         */
        @Override
        protected boolean wantOnSelectionChangedNotifications() {
            // we want roundtrips when a the user selects another item
            return true;
        }
    }

    private final class LocaleChoiceRenderer extends ChoiceRenderer {
        private static final long serialVersionUID = 1L;

        /**
         * @see org.apache.wicket.markup.html.form.IChoiceRenderer#getDisplayValue(Object)
         */
        @Override
        public Object getDisplayValue(final Object object) {
            Locale locale = (Locale) object;
            return locale.getDisplayName(ChooseLanguagePanel.this.getLocale());

        }
    }

    public void setLocale(final Locale locale) {
        if (locale != null) {
            this.getSession().setLocale(locale);
        }
    }

    public Class<? extends WPPage> getWPPage() {
        return wPPage;
    }

    public void setWPPage(final Class<? extends WPPage> wPPage) {
        this.wPPage = wPPage;
    }

}
